package cn.ibizlab.odoo.core.odoo_event.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ievent_mail_registration;
import cn.ibizlab.odoo.core.odoo_event.domain.Event_mail_registration;
import cn.ibizlab.odoo.core.odoo_event.filter.Event_mail_registrationSearchContext;
import cn.ibizlab.odoo.core.odoo_event.service.IEvent_mail_registrationService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[登记邮件调度] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Event_mail_registrationServiceImpl implements IEvent_mail_registrationService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ievent_mail_registrationClientService event_mail_registrationClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ievent_mail_registration clientModel = event_mail_registrationClientService.createModel();
        clientModel.setId(id);
        event_mail_registrationClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Event_mail_registration event_mail_registration) {
        boolean bOk = false ;
        Ievent_mail_registration clientModel = this.convert2Model(event_mail_registration,null);
        event_mail_registrationClientService.update(clientModel) ;
        this.convert2Domain(clientModel,event_mail_registration) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Event_mail_registration> event_mail_registrations, int batchSize) {

                        //event_mail_registrationClientService.createBatch(event_mail_registrations,batchSize) ;

    }

    @Override
    public void updateBatch(List<Event_mail_registration> event_mail_registrations, int batchSize) {

        //event_mail_registrationClientService.updateBatch(event_mail_registrations,batchSize) ;

    }

    @Override
    public boolean create(Event_mail_registration event_mail_registration) {
        boolean bOk = false ;
        Ievent_mail_registration clientModel = this.convert2Model(event_mail_registration,null);
        event_mail_registrationClientService.create(clientModel) ;
        this.convert2Domain(clientModel,event_mail_registration);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //event_mail_registrationClientService.removeBatch(event_mail_registrations) ;

    }

    @Override
    public Event_mail_registration get(Integer id) {
        Ievent_mail_registration clientModel = event_mail_registrationClientService.createModel();
        clientModel.setId(id);
        event_mail_registrationClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Event_mail_registration get(Event_mail_registration event_mail_registration) {
        Event_mail_registration retevent_mail_registration = this.get(event_mail_registration.getId()) ;
    	BeanCopier copier=BeanCopier.create(retevent_mail_registration.getClass(), event_mail_registration.getClass(), false);
        copier.copy(retevent_mail_registration, event_mail_registration, null);
        return event_mail_registration ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Event_mail_registration> searchDefault(Event_mail_registrationSearchContext context) {
        List<Event_mail_registration> list = new ArrayList<Event_mail_registration>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_mail_registration> clientModelList = event_mail_registrationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_mail_registration clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_mail_registration>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据自动发邮件查询
     */
    @Override
    public Page<Event_mail_registration> selectByOdoo_scheduler(Integer id){
        Event_mail_registrationSearchContext context = new Event_mail_registrationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_mail_registration> list = new ArrayList<Event_mail_registration>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_mail_registration> clientModelList = event_mail_registrationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_mail_registration clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_mail_registration>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据事件记录查询
     */
    @Override
    public Page<Event_mail_registration> selectByOdoo_registration(Integer id){
        Event_mail_registrationSearchContext context = new Event_mail_registrationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_mail_registration> list = new ArrayList<Event_mail_registration>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_mail_registration> clientModelList = event_mail_registrationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_mail_registration clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_mail_registration>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Event_mail_registration> selectByOdoo_create(Integer id){
        Event_mail_registrationSearchContext context = new Event_mail_registrationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_mail_registration> list = new ArrayList<Event_mail_registration>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_mail_registration> clientModelList = event_mail_registrationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_mail_registration clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_mail_registration>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Event_mail_registration> selectByOdoo_write(Integer id){
        Event_mail_registrationSearchContext context = new Event_mail_registrationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_mail_registration> list = new ArrayList<Event_mail_registration>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_mail_registration> clientModelList = event_mail_registrationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_mail_registration clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_mail_registration>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ievent_mail_registration convert2Model(Event_mail_registration domain , Ievent_mail_registration model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = event_mail_registrationClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getMail_sentDirtyFlag())
            model.setMail_sent(domain.getMail_sent());
         if(domain.getScheduled_dateDirtyFlag())
            model.setScheduled_date(domain.getScheduled_date());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getRegistration_id_textDirtyFlag())
            model.setRegistration_id_text(domain.getRegistration_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getRegistration_idDirtyFlag())
            model.setRegistration_id(domain.getRegistration_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getScheduler_idDirtyFlag())
            model.setScheduler_id(domain.getScheduler_id());
        return model ;
    }

    public Event_mail_registration convert2Domain( Ievent_mail_registration model ,Event_mail_registration domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Event_mail_registration();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getMail_sentDirtyFlag())
            domain.setMail_sent(model.getMail_sent());
        if(model.getScheduled_dateDirtyFlag())
            domain.setScheduled_date(model.getScheduled_date());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getRegistration_id_textDirtyFlag())
            domain.setRegistration_id_text(model.getRegistration_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getRegistration_idDirtyFlag())
            domain.setRegistration_id(model.getRegistration_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getScheduler_idDirtyFlag())
            domain.setScheduler_id(model.getScheduler_id());
        return domain ;
    }


}
